<?php

namespace App\Http\Controllers\Admin\Promotion;

use App\Http\Models\Admin\Sys\AdPosition;
use App\Http\Models\Admin\Sys\Advertisement;
use App\Http\Models\Admin\Sys\SysParam;
use App\Http\Responses\ResponseDto;
use App\Services\Admin\AdService;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Psy\Util\Json;

class AdController extends Controller
{

    public function __construct(AdService $service){
        $this->service = $service;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response{cate}/{subcategory}
     */
    public function index(Request $request)
    {
        return View::make('admin.promotion.ad')
            ->withLocations(AdPosition::all())
            ->withPagemodal(Config::get("common.ads_page"))
            ->withAssets($request['ASSETS']);
    }
    public function page(Request $request)
    {
        $datas = $request->input();
        $start = $datas[3]['value'];
        $length = $datas[4]['value'];
        $res = DB::table('ads')->select("id",'name',"ad_position","state","desc","created_at")->skip($start)->take($length)->get();
        $total = DB::table('ads')->count();
        foreach($res as  $item){
            $potion = AdPosition::select('name')->where('id',$item->ad_position)->first();
            if($potion){
                $item->positon =$potion->name;
            }
        }


        return ResponseDto::createResObj(ResponseDto::convertPageToDataTableObj($res,$total),ResponseDto::$SUCCESS);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }



    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $validator = Validator::make(Input::all(),
            [
                'name' => 'required',
                'ad_position' => 'required',
            ]);


        if($validator->fails()){
            return ResponseDto::createResObj($validator->errors()->first(),ResponseDto::$FAIL);
        }else {

            $ad = new Advertisement();
            $ad->ad_position = $request->input("ad_position");
            $ad->ad_content = $request->input("ad_content");
            $ad->name = $request->input("name");
            $ad->desc = $request->input("desc");

            if ($request->input("id")) {
                //更新操作
                $exit_item = Advertisement::where('id','<>',$request->input("id"))->where('name',$ad->name)->first();
                if($exit_item){
                    return ResponseDto::createResObj(trans("admin/message.name_exit"),ResponseDto::$FAIL);
                }
                else {
                    $exit_item = Advertisement::find($request->input("id"));
                    $exit_item->ad_position = $ad->ad_position;
                    $exit_item->ad_content = $ad->ad_content;
                    $exit_item->name = $ad->name;
                    $exit_item->desc = $ad->desc;
                    $exit_item->save();
                    return ResponseDto::createResObj(trans("admin/message.update_success"), ResponseDto::$SUCCESS);
                }
            } else {
                $find = Advertisement::where('name', $ad->name)->first();
                if($find){
                    return ResponseDto::createResObj(trans("admin/message.name_exit"),ResponseDto::$FAIL);
                }
                $ad->save();
                return ResponseDto::createResObj(trans("admin/message.submit_success"), ResponseDto::$SUCCESS);
            }

        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        return Json::encode(Advertisement::find($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Advertisement::destroy($id);
        return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
    }
}
